/**
 * ZHU framework. Copyright 2012-, all rights reserved.
 *
 * $Id: HttpWebRequestContext.java 24 2012-08-17 07:21:26Z zhuxiaopeng $
 * $Revision: 24 $
 * $Date: 2012-08-17 15:21:26 +0800 (五, 2012-08-17) $
 * $Author: zhuxiaopeng $
 */
package zhu.framework.web.servlet;

import java.util.*;

import javax.faces.application.*;
import javax.faces.context.*;
import javax.servlet.http.*;

import zhu.framework.service.*;
import zhu.framework.util.*;
import zhu.framework.web.*;

/**
 * <p>
 * </p>
 * 
 * @author $Author: zhuxiaopeng $
 * @version $Revision: 24 $ - $Date: 2012-08-17 15:21:26 +0800 (五, 2012-08-17) $
 */
public final class HttpWebRequestContext extends AbstractWebRequestContext {

	/**
	 * <p>
	 * </p>
	 * 
	 * @param serviceRequestContext
	 * @param servletRequest
	 */
	public HttpWebRequestContext(final ServiceRequestContext serviceRequestContext, final HttpServletRequest servletRequest,
			final WebSessionContext sessionContext) {
		super(serviceRequestContext, sessionContext);
		MyUtility.checkMethodParameterNotNull("servletRequest", servletRequest);
		//
		this.servletRequest = servletRequest;
		//
	}

	@Override
	public Object getAttribute(final String name) {
		MyUtility.checkMethodParameterNotNull("name", name);
		//
		return getServletRequest().getAttribute(name);
	}

	@Override
	public void setAttribute(final String name, final Object value) {
		MyUtility.checkMethodParameterNotNull("name", name);
		MyUtility.checkMethodParameterNotNull("value", value);
		//
		getServletRequest().setAttribute(name, value);
	}

	@Override
	public void removeAttribute(final String name) {
		MyUtility.checkMethodParameterNotNull("name", name);
		//
		getServletRequest().removeAttribute(name);
	}

	@Override
	public String getParameter(final String name) {
		MyUtility.checkMethodParameterNotNull("name", name);
		//
		return getServletRequest().getParameter(name);
	}

	@Override
	public boolean isResponseCompleted() {
		return FacesContext.getCurrentInstance().getResponseComplete();
	}

	@Override
	public void responseComplete() {
		final FacesContext facesContext = FacesContext.getCurrentInstance();
		// 以下は、「元のページのどのリンクをクリックしても再ダウンロードになる」JSFの動作を回避するためのコード
		final StateManager stateManager = facesContext.getApplication().getStateManager();
		stateManager.saveView(facesContext);
		// 以下が本質的なコード
		facesContext.responseComplete();
	}

	@Override
	public ResponseWriter getResponseWriter() {
		return FacesContext.getCurrentInstance().getResponseWriter();
	}

	@Override
	public Object getResponse() {
		return FacesContext.getCurrentInstance().getExternalContext().getResponse();
	}

	@Override
	public void addMessage(final WebMessage message) {
		addMessage(null, message);
	}

	@Override
	public void addMessage(final String clientId, final WebMessage message) {
		MyUtility.checkMethodParameterNotNull("message", message);
		//
		FacesContext.getCurrentInstance().addMessage(clientId, message.toFacesMessage());
	}

	@Override
	public void addMessages(final List<WebMessage> messages) {
		addMessages(null, messages);
	}

	@Override
	public void addMessages(final String clientId, final List<WebMessage> messages) {
		MyUtility.checkMethodParameterNotNull("messages", messages);
		//
		for (final WebMessage message : messages) {
			addMessage(clientId, message);
		}
	}

	/**
	 * <p>
	 * プロパティ<code>servletRequest</code>の値を取得する。
	 * </p>
	 * 
	 * @return プロパティ<code>servletRequest</code>の値
	 */
	private HttpServletRequest getServletRequest() {
		return servletRequest;
	}

	private final HttpServletRequest servletRequest;
}
